/**
 * 登陆进来后的首页用到的js和jQuery
 */

// 注意：导航 依赖 element 模块，否则无法进行功能性操作
layui.use('element', function () {
    var element = layui.element;
    element.render();
});

// 浏览他人主页时的跳转
$("span#otheruserName").click(function () {
    var userName = $(this).text();
    window.location.href = "/user/otheruserhome.do?userName=" + userName;
});

layui.use('layer', function () {
    var layer = layui.layer;
    $("[name='ptcv']").click(
        function () {
            var essayId = $(this).attr("essayId");
            var operate = $(this).attr("id");
            obj = $(this);
            /** ********点赞事件********************************************************** */
            if (operate == "praise") {
                /** **************Ajax进行数据传输以及局部更新******************* */
                $.ajax({
                    type: 'POST',
                    url: '/essay/ptcvactive.do',
                    data: {
                        flag: "praise",
                        essayId: essayId,
                    },
                    success: function (resl) {
                        if (resl == "praise") {//点赞成功
                            obj.text(parseInt(obj.text()) + 1);
                            obj.css('color', 'red');
                            layer.msg('赞一下');
                        } else if (resl == "unpraise") {//取消赞
                            var text = obj.text();
                            if (parseInt(text) > 0) {
                                obj.text(parseInt(obj.text()) - 1);
                                obj.css('color', 'black');
                                layer.msg('您之前对自文章点过赞，此操作取消了您的赞');
                            }
                        } else if (resl == "alreadytread") {//踩过，无法进行赞操作
                            layer.msg('您踩过此文章了呀，无法进点赞操作了');
                        } else if (resl == "false") {//出错了
                            layer.msg('服务器好像出错了', {
                                icon: 5
                            });
                        }
                    }
                });
            }
            /** ***********************踩事件********************************************* */
            else if (operate == "tread") {
                obj = $(this);
                /** **************Ajax进行数据传输以及局部更新******************* */
                $.ajax({
                    type: 'POST',
                    url: '/essay/ptcvactive.do',
                    data: {
                        flag: "tread",
                        essayId: essayId
                    },
                    success: function (rest) {
                        if (rest == "tread") {
                            obj.css('color', 'red');
                            obj.text(parseInt(obj.text()) + 1);
                            layer.msg('踩一下');
                        } else if (rest == "untread") {
                            var text = obj.text();
                            if (parseInt(text) > 0) {
                                obj.text(parseInt(obj.text()) - 1);
                                obj.css('color', 'black');
                                layer.msg('您之前踩过此文章，此操作取消了您的踩');
                            }
                        } else if (rest == "alreadypraise") {
                            layer.msg('您赞过此文章了呀，无法进行踩操作了');
                        } else if (rest == "false") {
                            layer.msg('服务器好像出错了', {
                                icon: 5
                            });
                        }
                    }
                });

            }
            /** ***************************评论事件***************************************** */
            else if (operate == "comment") {
                var essayId = $(this).attr("essayId");
                window.location.href = "/essay/essaydetail.do?essayId="
                    + essayId + "#comment";
            }
            /** ************真实度贡献事件弹窗贡献*********************************************** */
            else if (operate == "validity") {
                obj = $(this);
                var grade = $(this).attr("userGrade");
                // prompt层
                layer.prompt({
                    title: '请输入您认为此信息的的真实度，满分100',
                    formType: 0
                }, function (value, index) {
                    layer.close(index);
                    if (parseInt(value) > 100) {
                        layer.msg("贡献值有点大啊，好像输入错了");
                    } else {
                        /** **************Ajax进行数据传输以及局部更新******************* */
                        $.ajax({
                            type: 'POST',
                            url: '/essay/ptcvactive.do',
                            data: {
                                flag: "validity",
                                essayId: essayId,
                                value: value
                            },
                            success: function (resv) {
                                if (resv == "validity") {
                                    layer.msg('您认为此信息的真实度为' + value + '%'
                                        + '，感谢您为此做出贡献,我们将进行真实度转换相加', {
                                        icon: 6
                                    });
                                    var temp = obj.text();
                                    var a = new Number(temp.slice(0, temp.length - 1));
                                    var b = new Number((grade * 1 / 100) * value);
                                    var c = (a + b).toFixed(2);
                                    obj.text(c + "%");
                                    obj.css('color', 'red');
                                } else if (resv == "alreadyvalidity") {
                                    layer.msg("您已经为此文章做出过真实度评价，无法再进行修改");
                                } else if (resv == "false") {
                                    layer.msg('服务器好像出错了', {
                                        icon: 5
                                    });
                                }
                            }
                        });
                    }
                });
            }
        });
    /*****************************************搜索框***************************************************************/

    $("#Bsearch").click(function () {
        layer.open({
            type: 1
            , title: false
            , closeBtn: false
            , shadeClose: true
            , maxWidth: 10000
            , skin: 'fly-layer-search'
            , content: ['<form action="/common/search.do">'
                , '<input autocomplete="off" placeholder="搜索文章与用户，回车跳转" type="text" name="searchtext">'
                , '</form>'].join('')
            , success: function (layero) {
                var input = layero.find('input');
                input.focus();
                layero.find('form').submit(function () {
                    var val = input.val();
                    if (val.replace(/\s/g, '') === '') {
                        return false;
                    }
                    input.val(input.val());
                });
            }
        })
    });

    /*******************签到**************************/
    $("#signbutton").click(function () {
        $.ajax({
            type: 'POST',
            url: '/sign/sign.do',
            success: function (value) {
                var value = JSON.parse(value)
                if (value.result == "unlogin") {
                    layer.msg("您还未登录，请先登录", {icon: 2, time: 1000}, function () {
                        window.location.href = "/login.html";
                    });
                } else if (value.result == "alreadySign") {
                    layer.msg("今天已经签到过了呦", {icon: 1, time: 1000}, function () {
                        $("#sign").removeClass("layui-hide");
                        $("#unsign").addClass("layui-hide");
                    });
                } else if (value.result == "success") {
                    layer.msg("连续签到" + value.days + "天签到成功，恭喜获得" + value.gain + "积分", {
                        icon: 1,
                        time: 1000
                    }, function () {
                        $("#sign").removeClass("layui-hide");
                        $("#unsign").addClass("layui-hide");
                    });

                }
            }
        })
    });

    /*************************************分页（刷新分页实例）*******************************************/

    layui.use('laypage', function () {
        var laypage = layui.laypage;
        var category=$("#category").val();
        var orderby=$("#orderby").val();
        var totalCount=$("#TotalNumber").val();
        var currentPage=$("#page").val();
        //执行一个laypage实例
        laypage.render({
            elem: 'B_Page'
            , count: totalCount
            , limit: 10
            , curr: currentPage //获取起始页
            , first: '首页'
            , last: '尾页'
            , prev: '<em>←</em>'
            , next: '<em>→</em>'
            , skip: true
            , jump: function (obj, first) {
                if (!first) {
                    window.location.href="/essay/essayreadselect.do?currentPage="+obj.curr+"&category="+category+
                        "&orderby="+orderby;
                }
            }
        });
    });
});




